
package com.dodo.videoEdit.ui.filechoseactivity;

import android.Manifest;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;
import androidx.viewpager2.widget.ViewPager2;

import com.azhon.basic.base.BaseActivity;
import com.dodo.proiectCore.util.TipsUtil;
import com.dodo.videoEdit.R;
import com.dodo.videoEdit.databinding.ActivityFileChoseBinding;
import com.dodo.videoEdit.ui.fragment.FileChoseChildFragment;
import com.dodo.videoEdit.ui.fragment.adapters.FragmentPagerAdapter;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.ArrayList;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class FileChoseActivity extends BaseActivity<FileChoseViewModel, ActivityFileChoseBinding> {

    private int type = 0;
    private ArrayList<Fragment> fragList;


    @Override
    protected int onCreate() {
        return R.layout.activity_file_chose;
    }


    @Override
    protected FileChoseViewModel initViewModel() {
        return ViewModelProviders.of(this).get(FileChoseViewModel.class);
    }

    @Override
    protected void showError(Object obj) {

    }
    @Override
    protected void initView() {
        type = getIntent().getIntExtra("type", 0);
        //初始化内存数据
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.request(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE).
                subscribe(new Observer<Boolean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Boolean aBoolean) {
                        if (aBoolean) {
                            requestData();
                        } else {
                            TipsUtil.show("权限被拒绝,缺少必要权限用于对媒体文件操作!");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
        fragList = new ArrayList<>();
        if (type == 0) { //查询所有数据 都要显示
            fragList.add(FileChoseChildFragment.newInstance("视频", 3));
            fragList.add(FileChoseChildFragment.newInstance("图片", 2));
            fragList.add(FileChoseChildFragment.newInstance("音频", 1));
        } else if (type == 1) {//查询音频数据
            fragList.add(FileChoseChildFragment.newInstance("音频", 1));
        } else if (type == 2) { //查询图片数据
            fragList.add(FileChoseChildFragment.newInstance("图片", 2));
        } else if (type == 3) {//查询视频数据
            fragList.add(FileChoseChildFragment.newInstance("视频", 3));
        }
        FragmentPagerAdapter pageAdapter = new FragmentPagerAdapter(this,fragList);
        dataBinding.fileChoseContent.setAdapter(pageAdapter);
        dataBinding.fileChoseContent.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                dataBinding.topNavigationConstraint.setCurrentActiveItem(position);
            }
        });
        dataBinding.topNavigationConstraint.setNavigationChangeListener((view, position) -> dataBinding.fileChoseContent.setCurrentItem(position, true));
    }

    private void requestData() {
            viewModel.isDateBaseLoad();
    }

    @Override
    protected void initData() {

    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
